{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit VisualDescriptor;

interface

uses Classes, Colours, Fills, Plasma, Backgrounds, Foregrounds, Bitmaps,
     Fonts, Shapes, Captions, MediaWindows, PluginWindows, VButtons,
     UIGrids, MediaGrids, GridItems, Bars, TextEdits, Effects, Gauges, Dials,
     Descriptor;

const
  FILE_VISUAL_DESCRIPTOR = 'VisualDescriptor.xml';

type
  TAHMVisualDescriptor = class(TAHMDescriptor)
  private
    FColourList: TAHMColourList;
    FFillList: TAHMFillList;
    FPlasmaList: TAHMPlasmaList;
    FFontList: TAHMFontList;
    FBitmapList: TAHMBitmapList;
    FBackgroundList: TAHMBackgroundList;
    FForegroundList: TAHMForegroundList;
    FShapeList: TAHMShapeList;
    FCaptionList: TAHMCaptionList;
    FMediaWindowList: TAHMMediaWindowList;
    FPluginWindowList: TAHMPluginWindowList;
    FEffectList: TAHMEffectList;
    FBarList: TAHMAbstractBarList;
    FButtonList: TAHMButtonList;
    FTextEditList: TAHMTextEditList;
    FGridList: TAHMGridList;
    FGridItemList: TAHMGridItemList;
    FMediaGridList: TAHMMediaGridList;
    FGaugeList: TAHMGaugeList;
    FDialList: TAHMDialList;
  protected
    procedure SetBaseDescriptor(Base: TAHMDescriptor); override;
    function GetSaveRequired: Boolean; override;
    procedure ParseXML; override;
    procedure SaveAsXML(Lines: TStrings); override;
  public
    constructor Create; override;
    destructor Destroy; override;
    property ColourList: TAHMColourList read FColourList;
    property FillList: TAHMFillList read FFillList;
    property PlasmaList: TAHMPlasmaList read FPlasmaList;
    property FontList: TAHMFontList read FFontList;
    property BitmapList: TAHMBitmapList read FBitmapList;
    property BackgroundList: TAHMBackgroundList read FBackgroundList;
    property ForegroundList: TAHMForegroundList read FForegroundList;
    property ShapeList: TAHMShapeList read FShapeList;
    property CaptionList: TAHMCaptionList read FCaptionList;
    property MediaWindowList: TAHMMediaWindowList read FMediaWindowList;
    property PluginWindowList: TAHMPluginWindowList read FPluginWindowList;
    property EffectList: TAHMEffectList read FEffectList;
    property GridList: TAHMGridList read FGridList;
    property GridItemList: TAHMGridItemList read FGridItemList;
    property MediaGridList: TAHMMediaGridList read FMediaGridList;
    property BarList: TAHMAbstractBarList read FBarList;
    property ButtonList: TAHMButtonList read FButtonList;
    property TextEditList: TAHMTextEditList read FTextEditList;
    property GaugeList: TAHMGaugeList read FGaugeList;
    property DialList: TAHMDialList read FDialList;
    procedure Initialise; override;
    procedure Assign(Source: TAHMDescriptor); override;
    procedure Sort; override;
  end;

implementation

uses Screen, TagNames, XMLTags, ErrorCodes, SysUtils, Logging, Canvas, XMLHelper;

constructor TAHMVisualDescriptor.Create;
begin
  inherited Create;

  FFileName := FILE_VISUAL_DESCRIPTOR;
  FRootTagName := TAG_VISUAL_DESCRIPTOR;

  FColourList       := TAHMColourList.Create;
  FFillList         := TAHMFillList.Create;
  FPlasmaList       := TAHMPlasmaList.Create;
  FFontList         := TAHMFontList.Create;
  FBitmapList       := TAHMBitmapList.Create;
  FBackgroundList   := TAHMBackgroundList.Create;
  FForegroundList   := TAHMForegroundList.Create;
  FShapeList        := TAHMShapeList.Create;
  FCaptionList      := TAHMCaptionList.Create;
  FMediaWindowList  := TAHMMediaWindowList.Create;
  FPluginWindowList := TAHMPluginWindowList.Create;
  FEffectList       := TAHMEffectList.Create;
  FGridList         := TAHMGridList.Create;
  FGridItemList     := TAHMGridItemList.Create;
  FMediaGridList    := TAHMMediaGridList.Create;
  FBarList          := TAHMAbstractBarList.Create;
  FButtonList       := TAHMButtonList.Create;
  FTextEditList     := TAHMTextEditList.Create;
  FGaugeList        := TAHMGaugeList.Create;
  FDialList         := TAHMDialList.Create;
end;

destructor TAHMVisualDescriptor.Destroy;
begin
  FColourList.FreeWithContents;
  FFillList.FreeWithContents;
  FPlasmaList.FreeWithContents;
  FFontList.FreeWithContents;
  FBitmapList.FreeWithContents;
  FBackgroundList.FreeWithContents;
  FForegroundList.FreeWithContents;
  FShapeList.FreeWithContents;
  FCaptionList.FreeWithContents;
  FMediaWindowList.FreeWithContents;
  FPluginWindowList.FreeWithContents;
  FGridList.FreeWithContents;
  FGridItemList.FreeWithContents;
  FMediaGridList.FreeWithContents;
  FBarList.FreeWithContents;
  FButtonList.FreeWithContents;
  FTextEditList.FreeWithContents;
  FEffectList.FreeWithContents;
  FGaugeList.FreeWithContents;
  FDialList.FreeWithContents;

  inherited Destroy;
end;

procedure TAHMVisualDescriptor.SetBaseDescriptor(Base: TAHMDescriptor);
begin
  // Set base list property for each of our list objects
  with Base as TAHMVisualDescriptor do
  begin
    Self.FColourList.BaseList := ColourList;
    Self.FFillList.BaseList := FillList;
    Self.FPlasmaList.BaseList := PlasmaList;
    Self.FFontList.BaseList := FontList;
    Self.FBitmapList.BaseList := BitmapList;
    Self.FBackgroundList.BaseList := BackgroundList;
    Self.FForegroundList.BaseList := ForegroundList;
    Self.FShapeList.BaseList := ShapeList;
    Self.FCaptionList.BaseList := CaptionList;
    Self.FMediaWindowList.BaseList := MediaWindowList;
    Self.FPluginWindowList.BaseList := PluginWindowList;
    Self.FEffectList.BaseList := EffectList;
    Self.FGridList.BaseList := GridList;
    Self.FGridItemList.BaseList := GridItemList;
    Self.FMediaGridList.BaseList := MediaGridList;
    Self.FBarList.BaseList := BarList;
    Self.FButtonList.BaseList := ButtonList;
    Self.FTextEditList.BaseList := TextEditList;
    Self.FGaugeList.BaseList := GaugeList;
    Self.FDialList.BaseList := DialList;
  end;
end;

function TAHMVisualDescriptor.GetSaveRequired: Boolean;
begin
  // Save required if any of the lists are populated
  Result := FColourList.RequiresSaving or FFillList.RequiresSaving or
            FPlasmaList.RequiresSaving or FFontList.RequiresSaving or
            FBitmapList.RequiresSaving or FBackgroundList.RequiresSaving or
            FForegroundList.RequiresSaving or FShapeList.RequiresSaving or
            FCaptionList.RequiresSaving or FMediaWindowList.RequiresSaving or
            FPluginWindowList.RequiresSaving or FEffectList.RequiresSaving or
            FGridList.RequiresSaving or FGridItemList.RequiresSaving or
            FMediaGridList.RequiresSaving or FBarList.RequiresSaving or
            FButtonList.RequiresSaving or FTextEditList.RequiresSaving or
            FGaugeList.RequiresSaving or FDialList.RequiresSaving;
end;

procedure TAHMVisualDescriptor.ParseXML;
var
  RootNode: TAHMXMLNode;
begin
  // What nodes do we have here?
  RootNode := FHelper.CurrentNode;
  FParseNode := nil;

  while FHelper.NextElement(RootNode, FParseNode) do
  begin
    if FHelper.CurrentElement = TAG_COLOURS then
      FColourList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_FILLS then
      FFillList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_PLASMAS then
      FPlasmaList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_FONTS then
      FFontList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_BITMAPS then
      FBitmapList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_BACKGROUNDS then
      FBackgroundList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_FOREGROUNDS then
      FForegroundList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_SHAPES then
      FShapeList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_CAPTIONS then
      FCaptionList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_MEDIAWINDOWS then
      FMediaWindowList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_PLUGINWINDOWS then
      FPluginWindowList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_EFFECTS then
      FEffectList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_GRIDS then
      FGridList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_GRID_ITEMS then
      FGridItemList.ParseFromXML(FHelper)   
    else if FHelper.CurrentElement = TAG_MEDIAGRIDS then
      FMediaGridList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_BARS then
      FBarList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_BUTTONS then
      FButtonList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_TEXTEDITS then
      FTextEditList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_GAUGES then
      FGaugeList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_DIALS then
      FDialList.ParseFromXML(FHelper)
    else
      LogParsingError(THM_PRS_VIS_FOUND_UNKNOWN_CONTENT_TAG);
  end;
end;

procedure TAHMVisualDescriptor.SaveAsXML(Lines: TStrings);
begin
  // Write all visual descriptor lists to XML file
  FColourList.SaveAsXML(Lines);
  FFillList.SaveAsXML(Lines);
  FPlasmaList.SaveAsXML(Lines);
  FFontList.SaveAsXML(Lines);
  FBitmapList.SaveAsXML(Lines);
  FBackgroundList.SaveAsXML(Lines);
  FForegroundList.SaveAsXML(Lines);
  FShapeList.SaveAsXML(Lines);
  FCaptionList.SaveAsXML(Lines);
  FMediaWindowList.SaveAsXML(Lines);
  FPluginWindowList.SaveAsXML(Lines);
  FEffectList.SaveAsXML(Lines);
  FGridItemList.SaveAsXML(Lines);
  FGridList.SaveAsXML(Lines);
  FMediaGridList.SaveAsXML(Lines);
  FBarList.SaveAsXML(Lines);
  FButtonList.SaveAsXML(Lines);
  FTextEditList.SaveAsXML(Lines);
  FGaugeList.SaveAsXML(Lines);
  FDialList.SaveAsXML(Lines);
end;

procedure TAHMVisualDescriptor.Initialise;
begin
  inherited;

  // Load required bitmaps for descriptor
  FBitmapList.LoadBitmaps;
end;

procedure TAHMVisualDescriptor.Assign(Source: TAHMDescriptor);
begin
  inherited;

  // Copy properties from visual descriptor
  with Source as TAHMVisualDescriptor do
  begin
    Self.ColourList.AssignContents(ColourList);
    Self.FillList.AssignContents(FillList);
    Self.PlasmaList.AssignContents(PlasmaList);
    Self.FontList.AssignContents(FontList);
    Self.BitmapList.AssignContents(BitmapList);
    Self.BackgroundList.AssignContents(BackgroundList);
    Self.ForegroundList.AssignContents(ForegroundList);
    Self.ShapeList.AssignContents(ShapeList);
    Self.CaptionList.AssignContents(CaptionList);
    Self.MediaWindowList.AssignContents(MediaWindowList);
    Self.PluginWindowList.AssignContents(PluginWindowList);
    Self.EffectList.AssignContents(EffectList);
    Self.GridList.AssignContents(GridList);
    Self.GridItemList.AssignContents(GridItemList);
    Self.MediaGridList.AssignContents(MediaGridList);
    Self.BarList.AssignContents(BarList);
    Self.ButtonList.AssignContents(ButtonList);
    Self.TextEditList.AssignContents(TextEditList);
    Self.GaugeList.AssignContents(GaugeList);
    Self.DialList.AssignContents(DialList);
  end;
end;

procedure TAHMVisualDescriptor.Sort;
begin
  inherited;

{$IFDEF DESIGNER}
  FColourList.SortObjects;
  FFillList.SortObjects;
  FPlasmaList.SortObjects;
  FFontList.SortObjects;
  FBitmapList.SortObjects;
  FBackgroundList.SortObjects;
  FForegroundList.SortObjects;
  FShapeList.SortObjects;
  FCaptionList.SortObjects;
  FMediaWindowList.SortObjects;
  FPluginWindowList.SortObjects;
  FEffectList.SortObjects;
  FBarList.SortObjects;
  FButtonList.SortObjects;
  FTextEditList.SortObjects;
  FGridList.SortObjects;
  FGridItemList.SortObjects;
  FMediaGridList.SortObjects;
  FGaugeList.SortObjects;
  FDialList.SortObjects;
{$ENDIF}
end;

end.


